﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Sw;
using FytSoa.Extensions;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Api.Controllers.Shop
{
    [Route("api/shop/[controller]")]
    [ApiController]
    [JwtAuthorize(Roles = "Admin")]
    public class CouponController : ControllerBase
    {
        private readonly ISw_couponService _couponService;
        private readonly ISw_couponlogService _logService;
        private readonly IMember_GroupService _groupService;
        public CouponController(ISw_couponService couponService
            , ISw_couponlogService logService
            , IMember_GroupService groupService)
        {
            _couponService = couponService;
            _logService = logService;
            _groupService = groupService;
        }

        /// <summary>
        /// 查询优惠券生成详情列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("loglist")]
        public async Task<IActionResult> GetCouponLogList([FromQuery]PageParm param)
        {
            return Ok(await _logService.GetPageCouponLog(param));
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<IActionResult> GetCouponList([FromQuery]PageParm param)
        {
            return Ok(await _couponService.GetPagesAsync(param, m => true, m => m.AddTime, Common.DbOrderEnum.Desc));
        }

        /// <summary>
        /// 根据id获得信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("getmodel")]
        public async Task<IActionResult> GetModel([FromBody]ParmString obj)
        {
            var couponModel = await _couponService.GetModelAsync(m => m.Guid == obj.parm);
            var typeList = await _groupService.GetListAsync(m=>m.Types==2,m=>m.Level,DbOrderEnum.Asc);
            var resDto = new SwCouponDto()
            {
                model = couponModel.data,
                list = typeList.data
            };
            return Ok(new ApiResult<SwCouponDto>()
            {
                data = resDto,
                statusCode = couponModel.statusCode,
                message = couponModel.message
            });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> Add([FromBody]Sw_coupon model)
        {
            return Ok(await _couponService.Add(model));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<IActionResult> Update([FromBody]Sw_coupon model)
        {
            return Ok(await _couponService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Delete([FromBody]ParmString obj)
        {
            var list = Utils.StrToListString(obj.parm);
            await _logService.DeleteAsync(m=>list.Contains(m.CouponGuid));
            return Ok(await _couponService.DeleteAsync(obj.parm));
        }
    }
}